﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using SqlSugar;
using YJTTool.DBBaseServer.Configurations;
using YJTTool.DBBaseServer.Interfaces;

namespace YJTTool.DBBaseServer.Services
{
    public class BaseRepository<T> : IBaseRepository<T> where T : class, new()
    {
        private readonly SqlSugarClient _db;

        public BaseRepository()
        {
            _db = SqlSugarConfiguration.GetInstance();
        }

        public List<T> Query()
        {
            return _db.Queryable<T>().ToList(); // 异步查询
        }

        public async Task<T> GetByIdAsync(int id)
        {
            return await _db.Queryable<T>().InSingleAsync(id);
        }

        public async Task InsertAsync(T entity)
        {
            await _db.Insertable(entity).ExecuteCommandAsync();
        }

        public async Task UpdateAsync(T entity)
        {
            await _db.Updateable(entity).ExecuteCommandAsync();
        }

        public async Task DeleteIdAsync(int id)
        {
            await _db.Deleteable<T>().In(id).ExecuteCommandAsync();
        }
        public async Task<int> DeleteByWhereAsync(Expression<Func<T, bool>> whereExpression)
        {
            return await _db.Deleteable<T>() .Where(whereExpression) .ExecuteCommandAsync();
        }
    }
}
